<?php

require_once 'UserList.php';
require_once 'RestaurantList.php';
require_once 'ChainList.php';
require_once 'MenuPositionList.php';
require_once 'OrderList.php';
require_once 'OrderMealList.php';
require_once 'StaffList.php';

/**
 * Klasa główna modułu zarządzania bazą danych.
 *
 * @author Rafał Cupał
 * @author Przemysław Furmańczyk
 */
class DataBase {

    protected $pdo;

    public function connect($host, $port, $dataBaseName, $user, $password) {
        try {
            $this->pdo = new PDO('mysql:host=' . $host . ';port=' . $port . 
                    ';dbname=' . $dataBaseName, $user, $password, 
                    array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'));
            $this->pdo->setAttribute(PDO::ATTR_ERRMODE, 
                    PDO::ERRMODE_EXCEPTION);
            $this->pdo->setAttribute(PDO::ATTR_AUTOCOMMIT, true);
        } catch (PDOException $e) {
            throw new Exception('Failed to connect to the database server! - ' . 
                    $e->getMessage());
        }
    }

    public function query($command) {
        try {
            if (isset($this->pdo)) {
                $statement = $this->pdo->query($command);
                return $statement;
            }
        }
        catch(PDOException $e) {
            throw new Exception('Database query error! - ' . $command . ' - ' . 
                    $e->getMessage());
        }
    }

    public function exec($command) {
        try {
            if (isset($this->pdo)) {
                $numChanges = $this->pdo->exec($command);
                return $numChanges;
            }
        }
        catch(PDOException $e) {
            throw new Exception('Database query error! - ' . $command . ' - ' . 
                    $e->getMessage());
        }
    }
    
    public function safeText($text, $addQuotes = true) {
        return ($addQuotes ? '\'' : '') . 
                strtr($text, array('\'' => '\\\'', 
                '"' => '\\"', '\\' => '\\\\')) . 
                ($addQuotes ? '\'' : '');
    }
    
    public function lastInsertId() {
        return $this->pdo->lastInsertId();
    }
    
    public function insert($tableName, $data) {
        $command_columns = '';
        $command_values = '';
        
        foreach ($data as $column => $value) {
            if ($command_columns != '') {
                $command_columns .= ',';
                $command_values .= ',';
            }
            $command_columns .= $column;
            $command_values .= $this->safeText($value);
        }
        
        $command = 'INSERT INTO ' . $tableName . ' (' .
                $command_columns . ') VALUES(' . $command_values . ')';

        return $this->Exec($command);
    }
    
    public function update($tableName, $data, $condition = '') {
        $command = 'UPDATE ' . $tableName . ' SET ';
        
        $first_column = true;
        foreach ($data as $column => $value) {
            if ($first_column) {
                $first_column = false;
            }
            else {
                $command .= ',';
            }
            $command .= $column . ' = ' . $this->safeText($value);
        }
        
        if ($condition != '') {
            $command .= ' WHERE ' . $condition;
        }

        return $this->Exec($command);
    }
    
    public function getUser() {
        return new User($this);
    }
    
    public function getUserList() {
        return new UserList($this);
    }
    
    public function getRestaurant() {
        return new Restaurant($this);
    }
    
    public function getRestaurantList() {
        return new UserList($this);
    }
    
    public function getChain() {
        return new Chain($this);
    }
    
    public function getChainList() {
        return new ChainList($this);
    }
    
    public function getMenuPosition() {
        return new MenuPosition($this);
    }
    
    public function getMenuPositionList() {
        return new MenuPositionList($this);
    }
    
    public function getOrder() {
        return new Order($this);
    }
    
    public function getOrderList() {
        return new OrderList($this);
    } 
    
    public function getStaff() {
        return new Staff($this);
    }
    
    public function getStaffList() {
        return new StaffList($this);
    }     
    
    public function getSetting($name) {
        $value = null;
        $statement = $this->query('SELECT ' . $name . ' FROM settings LIMIT 1');
        if ($row = $statement->fetch()) {
            $value = $row[$name];
        }
        $statement->closeCursor();
        return $value;
    }
    
    public function setSetting($name, $value) {
        $this->update("settings", array($name => $value));
    }
}

